<template>
  <div class="payment_wrap">
    <div class="con flex_center">
      <div class="payment_box">
        <div class="top">当前用户：<span class="num_color">张张张</span></div>
        <div class="order_item">
          <span>订单号：</span><span>{{ orderInfo.orderId }}</span>
        </div>
        <div class="order_item">
          <span>商品名称：</span><span>{{ orderInfo.productName }}</span>
        </div>
        <div class="order_item">
          <span>交易金额：</span><span>{{ orderInfo.amount }}</span>
        </div>
        
        <div v-if="paySuccessFlag=='SUCCESS'">
          <img src="../../assets/pay.png" alt="" class="pay_img" />
          <div class="pay_success">支付成功</div>
          <div class="btn_wrap flex_center">
            <el-button type="primary" @click="goBack">返回首页</el-button>
          </div>
        </div>
        <div v-else>
          <div class="price">{{ orderInfo.amount }}</div>
          <div class="qrcode" id="qrcodeImg" ref="qrcodeImg"></div>
          <div class="wx_tip flex_center">
            <img src="../../assets/wx.png" class="wx_img" alt="" />
            微信支付
          </div>
          <div class="btn_wrap flex_center">
            <el-button type="success" @click="getStatus">支付完成</el-button>
            <el-button type="primary" @click="init">刷新二维码</el-button>
          </div>
        </div>
      </div>
      <AppFooter class="footer" />
    </div>
  </div>
</template>
<script>
import AppFooter from "../../layout/components/AppFooter.vue";
import QRCode from "qrcodejs2";
import { payOrder,getPayStatus } from "@/api/payment.js";
export default {
  name: "",
  components: {
    AppFooter,
  },
  data() {
    return {
      orderInfo: {},
      paySuccessFlag: '',
      params: {clz:'',amunt:0},
    };
  },
  created() {
    
  },
  mounted() {
    let obj = JSON.parse(this.$route.query.row)
    this.params.clz = obj.clz
    this.params.amunt = obj.price
    this.init();
  },
  methods: {
    init() {
      payOrder(this.params).then((res) => {
        if (res.code == 200) {
          this.orderInfo = res.data;
            this.qrcode(this.orderInfo.codeUrl);
        }
      });
    },
    qrcode(url) {
      this.$refs.qrcodeImg.innerHTML = ''
      return new QRCode("qrcodeImg", {
        width: 160,
        height: 160,
        text: url,
        colorDark: "#000",
        colorLight: "#fff",
      });
    },
    getStatus(){
      let params ={
        orderId:this.orderInfo.orderId
      }
      getPayStatus(params).then(res=>{
        if (res.code == 200) {
          this.paySuccessFlag = res.data.resultStatus
          if(this.paySuccessFlag!='SUCCESS'){
            this.$message({
              message: '该订单暂未支付，请扫码支付！',
              type: 'warning'
            })
            return false
          }
        }
      })
    },
    goBack() {
      this.$router.replace({ path: "/" });
    },
  },
};
</script>

<style lang="scss">
.payment_wrap {
  width: 100%;
  background-color: #f6f7f8;
  height: 100vh;
  .con {
    width: 100%;
    height: calc(100vh - 120px);
  }
  .footer {
    position: fixed;
    bottom: 0;
  }
  .payment_box {
    width: 900px;
    font-size: 14px;
    line-height: 24px;
    color: #666666;
    margin: 0 auto;
    background-color: #fff;
    box-shadow: 0 2px 12px 0 rgba(96, 158, 215, 0.3);
    .top {
      background-color: rgba(0, 145, 255, 0.1);
      padding: 8px 20px;
      text-align: right;
      margin-bottom: 10px;
    }
    .order_item {
      padding-left: 30px;
    }
    .price {
      font-size: 30px;
      font-weight: bold;
      padding: 20px;
      text-align: center;
      color: #f60;
    }
    .qrcode {
      width: 200px;
      height: 200px;
      margin: 0 auto;
      border: 1px solid #4ab415;
      padding: 20px;
    }
    .wx_tip {
      padding: 20px;
      font-size: 18px;
      .wx_img {
        height: 30px;
        margin-right: 10px;
      }
    }
    .btn_wrap {
      width: 300px;
      margin: 0 auto;
      padding-bottom: 30px;
      .el-button {
        margin: 10px 30px;
      }
    }
    .pay_img {
      width: 150px;
      height: 150px;
      display: block;
      margin: 0 auto;
    }
    .pay_success {
      font-size: 20px;
      /* font-weight: bold; */
      padding: 20px 0 10px;
      text-align: center;
    }
  }
}
</style>
